package lzw.codec.ui;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import lzw.codec.exceptions.DecodingException;
import lzw.codec.exceptions.SystemException;
import lzw.codec.managers.LZWCodecManager;
import lzw.codec.managers.LZWFileManager;

/**
*  
* Classe que representa o frame onde será impresso a sequência decodificada.
*
* @author Gutemberg Rodrigues
*/
public class SequenceDecodedOutput extends javax.swing.JFrame {


	private LZWFileManager manager;
	private LZWCodecManager code;

    public SequenceDecodedOutput() {
        initComponents();
        manager = LZWFileManager.getInstance();
        code = LZWCodecManager.getInsntace();
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 
    }

     public void ini(){

        try {
            String sequenceEncodedOutput = code.decode(manager.loadEncodedFile("./files/sequenceEncodedInput.lzw"), manager.loadInitialDecoderDictionary("./files/dictionaryInitial.lzw"));
            painelTxt.setText(sequenceEncodedOutput);
            painelTxt.setEditable(false);
            
        }  catch (DecodingException e) {
        	
        	JOptionPane.showMessageDialog(this, e.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
		
        } catch (SystemException e) {
			
			JOptionPane.showMessageDialog(this, e.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
		}
       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")

    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        painelTxt = new javax.swing.JTextPane();
        bntAtualizar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Sequência Codificada"));

        jScrollPane2.setViewportView(painelTxt);

        bntAtualizar.setText("Atualizar Sequência");
        bntAtualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntAtualizarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                    .addComponent(bntAtualizar, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(bntAtualizar)
                .addContainerGap(11, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }

    private void bntAtualizarActionPerformed(java.awt.event.ActionEvent evt) {
        ini();
    }



    public void openFrameSequenceDecodedOutput(){

         SequenceDecodedOutput frameSequenceEncodedOutput = new SequenceDecodedOutput();
         frameSequenceEncodedOutput.ini();
         frameSequenceEncodedOutput.setVisible(true);
         frameSequenceEncodedOutput.setLocationRelativeTo(null);
    }

    private javax.swing.JButton bntAtualizar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextPane painelTxt;

}
